package com.zhou.spring.cloud.balancer.exception;


import lombok.Data;

/**
 * <pre>
 *     @author : loktar
 *     @e-mail : aakosp@gmail.com
 *     @time   : 2020-04-20
 *     @desc   :
 *     @version: 1.0
 * </pre>
 */
@Data
public class Response<T> {
    private Integer code;
    private String msg;
    private T data;

    public Response() {
    }

    public Response(Integer code, String msg) {
        this(code, msg, null);
    }

    public Response(Integer code, String msg, T data) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static Response response(ResponseEnum responseEnum) {
        return new Response(responseEnum.getCode(), responseEnum.getName());
    }

    public static Response response(ResponseEnum responseEnum, Object obj) {
        return new Response(responseEnum.getCode(), responseEnum.getName(), obj);
    }

    public static Response success() {
        return new Response(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getName());
    }

    public static Response success(String msg) {
        return new Response(ResponseEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response();
        response.setCode(ResponseEnum.SUCCESS.getCode());
        response.setMsg(ResponseEnum.SUCCESS.getName());
        response.setData(data);
        return response;
    }

    public static Response error(ResponseEnum responseEnum) {
        return new Response(responseEnum.getCode(), responseEnum.getName());
    }

    public static Response error(String message) {
        return new Response(ResponseEnum.EXCEPTION.getCode(), message);
    }

    public static Response error(Integer code, String msg) {
        return new Response(code, msg);
    }

    public static Response build(ResponseEnum responseEnum) {
        return new Response(responseEnum.getCode(), responseEnum.getName());
    }

    public Boolean isSuccess() {
        if (code != null && code == 0) {
            return true;
        }
        return false;
    }
}
